package com.soft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.entity.TOrder;
import com.soft.dao.TOrderDao;
import com.soft.service.TOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-12-26 16:40:33
 */
@Service("tOrderService")
public class TOrderServiceImpl implements TOrderService {
    @Resource
    private TOrderDao tOrderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TOrder queryById(Integer id) {
        return this.tOrderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TOrder> queryAllByLimit(int offset, int limit) {
        return this.tOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tOrder 实例对象
     * @return 实例对象
     */
    @Override
    public TOrder insert(TOrder tOrder) {
        this.tOrderDao.insert(tOrder);
        return tOrder;
    }

    /**
     * 修改数据
     *
     * @param tOrder 实例对象
     * @return 实例对象
     */
    @Override
    public TOrder update(TOrder tOrder) {
        this.tOrderDao.update(tOrder);
        return this.queryById(tOrder.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tOrderDao.deleteById(id) > 0;
    }

    @Override
    public TOrder getOrderAndOrderItems(Integer orderId){
        return this.tOrderDao.getOrderAndOrderItem(null, orderId).get(0);
    }
    @Override
    public PageInfo<TOrder> getUserOrdersAndOrderItems(Integer pageNum, Integer pageSize, Integer userId){
        PageHelper.startPage(pageNum, pageSize);
        List<TOrder> list = this.tOrderDao.getOrderAndOrderItem(userId, null);
        PageInfo<TOrder> pageInfo = new PageInfo<TOrder>(list);
        return pageInfo;
    }

    @Override
    public List<TOrder> queryAllOrders() {
        return this.tOrderDao.queryAll(null);
    }

    @Override
    public PageInfo<TOrder> queryAllByPageHelper(int pageNum, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, pageSize);
        List<TOrder> list = this.tOrderDao.findAllOrders();
        PageInfo<TOrder> pageInfo = new PageInfo<TOrder>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TOrder> vagueFindAllOrders(Integer pageNum, Integer pageSize, String search){
        PageHelper.startPage(pageNum, pageSize);
        List<TOrder> list = this.tOrderDao.vagueFindAllOrders(search);
        PageInfo<TOrder> pageInfo = new PageInfo<TOrder>(list);
        return pageInfo;
    }

}